﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;

namespace EFong.View.TrainTicket
{
    public partial class Frm_TrainTicketReturn : BasicForm
    {
        private Proxy.BaseServiceProxy _baseProxy {get{ return Proxy.BaseServiceProxy.GetInstance();}}
        private Proxy.BusinessServiceProxy _businessProxy {get{ return Proxy.BusinessServiceProxy.GetInstance();}}

        public Frm_TrainTicketReturn()
        {
            InitializeComponent();
        }

        protected override void InitFields()
        {
            base.InitFields();
        }

        protected override void Events()
        {
            this.btn_OK.Click += (s, e) =>
                {
                    save();
                };
        }

        protected override void LoadDatas()
        {
            this.cmb_OrderUser.Properties.BindingData(_baseProxy.GetEffUser().ReturnValue);

            if (this.cmb_OrderUser.GetValue() == null)
                this.cmb_OrderUser.SetValue(AppParameters.LoginUser);
        }

        private void save()
        {
            UIOperManager.SaveData(
                () =>
                {
                    Contract.TrainTicketReturn detail = getDetail();
                    UpdateToDetail(detail);
                    return _businessProxy.ReturnTrainTicketOrder(Detail,OrderDetail,Order.TotalProfit.Value).IsSuccess;
                },
                "退票成功.",
                () =>
                {
                    DialogResult = DialogResult.OK;
                    this.Close();
                }
                );
        }

        #region Detail
        private Contract.TrainTicketReturn _detail;
        public Contract.TrainTicketReturn Detail
        {
            get { return _detail; }
            set
            {
                _detail = value;
                onDetailChanged();
            }
        }

        public Contract.TrainTicketOrder Order { get; set; }

        private Contract.TrainTicketOrderDetail _orderDetail;
        public Contract.TrainTicketOrderDetail OrderDetail
        {
            get { return _orderDetail; }
            set
            {
                _orderDetail = value;
                this.bds_trainTicketOrderDetail.BindingData(new List<Contract.TrainTicketOrderDetail> { OrderDetail });
                spe_Qty.SetValue(OrderDetail.Qty);
            }
        }

        private void onDetailChanged()
        {
            if (Detail == null)
            {
                this.bds_trainTicketOrderDetail.BindingData(new List<Contract.TrainTicketOrderDetail>());
            }
            else
            {
                if(!string.IsNullOrEmpty(Detail.Ref))
                    this.txt_OrderRef.SetText(Detail.Ref);

                if(Detail.OrderUser != null)
                    this.cmb_OrderUser.SetText(Detail.OrderUser);

                this.dte_OrderDate.SetValue(Detail.OrderDate);

                this.spe_Price.SetValue(Detail.VendorReturn);

                this.spe_CustomerPrice.SetValue(Detail.CustomerReturn);

                txt_Remark.SetText(Detail.Remark);
            }
        }

        private Contract.TrainTicketReturn getDetail()
        {
            Contract.TrainTicketReturn detail = new Contract.TrainTicketReturn();

            detail.OrderDate = dte_OrderDate.GetValue();
            detail.OrderUser = cmb_OrderUser.GetValue() as Contract.User;
            detail.VendorReturn = spe_Price.GetValue();
            detail.CustomerReturn = spe_CustomerPrice.GetValue();
            detail.Remark = txt_Remark.GetText();
            detail.Qty = (int)spe_Qty.GetValue();

            return detail;
        }

        private void UpdateToDetail(Contract.TrainTicketReturn detail)
        {
            Detail.OrderUser = detail.OrderUser;
            Detail.OrderDate = detail.OrderDate;
            Detail.Remark = detail.Remark;
            Detail.VendorReturn = detail.VendorReturn;
            Detail.CustomerReturn = detail.CustomerReturn;
            Detail.Qty = detail.Qty;
        }
        #endregion
    }
}
